//
//  BBBtDateUtil.m
//  BabyThermometer
//
//  Created by CaiLei on 10/11/14.
//  Copyright (c) 2014 baohu. All rights reserved.
//

#import "BBBtDateUtil.h"

@implementation BBBtDateUtil
+ (NSData *)dataFromHexString:(NSString *)hexString error:(NSError **)error {
    if (error != NULL) *error = nil;
    NSMutableData *retData = [NSMutableData data];
    
    
    NSString *trimSpaceString = [BBBtDateUtil trimSpaceInString:hexString];
    NSMutableString *hex = [NSMutableString stringWithString:[trimSpaceString lowercaseString]];
    if ([hex hasPrefix:@"0x"]) {
        hex = [NSMutableString stringWithString:[hex substringFromIndex:2]];
    }

    while (!isEmpty(hex)) {
        if (hex.length < 2) {
            if (error != NULL) *error = [NSError errorWithDomain:@"" code:-1 userInfo:@{@"error": @"need 2 digit in 1 byte for hex"}];
            return nil;
        }
        
        NSString *byteStr = [hex substringToIndex:2];
        hex = [NSMutableString stringWithString:[hex substringFromIndex:2]];
        
        NSScanner *scan = [NSScanner scannerWithString:byteStr];
        unsigned int byte = 0;
        if (![scan scanHexInt:&byte]) {
            NSString *msg = [NSString stringWithFormat:@"%@ is not a valid hex", byteStr];
            if (error != NULL) *error = [NSError errorWithDomain:@"" code:-1 userInfo:@{@"error": msg}];
            return nil;
        };
        
        uint8_t finalbyte = byte;
        [retData appendBytes:&finalbyte length:sizeof(finalbyte)];
    }
    
    return retData;
}

+ (NSString *)trimSpaceInString:(NSString *)input {
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
    NSArray *parts = [input componentsSeparatedByCharactersInSet:whitespaces];
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    return [filteredArray componentsJoinedByString:@""];
}

+ (Bt_Data_Type)dataTypeFor:(NSData *)data {
    if (isEmpty(data)) {
        return Bt_Data_Type_Unknown;
    }
    
    uint8_t *byte = (uint8_t *)[data bytes];
    
    if (1 == data.length) {
        if (Bt_Data_Type_Error == *byte) {
            return Bt_Data_Type_Error;
        }
        if (Bt_Data_Type_Ack == *byte) {
            return Bt_Data_Type_Ack;
        }
    }
    
    if (2 == data.length) {
        if (Bt_Data_Type_Temparature == *byte) {
            return Bt_Data_Type_Temparature;
        }
    }
    
    return Bt_Data_Type_Unknown;
}
+ (CGFloat)temparatureFromData:(NSData *)data {
    if ([BBBtDateUtil dataTypeFor:data] != Bt_Data_Type_Temparature) {
        return MAXFLOAT;
    }
    
    uint8_t *byte = (uint8_t *)[data bytes];
    CGFloat ret = (CGFloat)byte[1];
    
    if ((byte[1] == 0x63) || (byte[1] == 0x64)) {
        DDLogInfo(@"低于34 || 高于42");
        return ret;
    } else if ((byte[1] >= 0x00) && (byte[1] <= 0x50)) {
        ret = ret * 0.1 + 34.0;        
        DDLogInfo(@"%.1f", ret);
        return ret;
    } else {
        return MAXFLOAT;
    }
    
    return MAXFLOAT;
}
+ (NSData *)dataWithDate:(NSDate *)date {
    NSCalendar* calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *daycomponent = [calendar components:NSYearCalendarUnit |
                             NSMonthCalendarUnit |
                             NSDayCalendarUnit |
                             NSHourCalendarUnit |
                             NSMinuteCalendarUnit |
                             NSSecondCalendarUnit
                                        fromDate:date];
    

    uint8_t year = MAX(2000, [daycomponent year]);
    year = MIN(year, 2099);
    year -= 2000;
    
    uint8_t month = [daycomponent month];
    uint8_t day = [daycomponent day];
    uint8_t hour = [daycomponent hour];
    uint8_t minute = [daycomponent minute];
    uint8_t second = [daycomponent second];
    
    
    NSString *hexString = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x", 0x41, year, month, day, hour, minute, second];
    return [BBBtDateUtil dataFromHexString:hexString error:nil];
}

@end
